package com.hzeport.bgs22emduty.outbound.db;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.dto.PersonInfoDTO;
import com.hzeport.bgs22emduty.entity.BaseGroupInfo;
import com.hzeport.bgs22emduty.entity.BaseGroupUserRelation;
import com.hzeport.bgs22emduty.entity.ParamGroupDuty;
import com.hzeport.bgs22emduty.util.GuavacacheUtils;
import com.hzeport.bgs22emduty.vo.base.PersonInfoData;
import com.hzeport.bgs22emduty.common.base.BaseRepositoryImpl;
import com.hzeport.bgs22emduty.entity.BasePersonInfo;
import com.hzeport.bgs22emduty.outbound.db.mapper.BasePersonInfoMapper;
import com.hzeport.bgs22emduty.outbound.BasePersonInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2022-12-02 11:21:59
 */
@Service
@Slf4j
public class BasePersonInfoRepositoryImpl extends BaseRepositoryImpl<BasePersonInfoMapper, BasePersonInfo> implements BasePersonInfoRepository {

    @Resource
    private BasePersonInfoMapper basePersonInfoMapper;
    @Resource
    private GuavacacheUtils guavacacheUtils;

    @Override
    public Page<PersonInfoData> getPersonInfo(PersonInfoDTO dto) {
        Page<PersonInfoData> page=new Page<>(dto.getPageNum(),dto.getPageSize());
        return basePersonInfoMapper.getPersonInfo(page);
    }

    @Override
    public List<BasePersonInfo> getPersonInCustomGroup(String customId, String type, String dutyType) {
        log.info("排班配置：customId="+customId+"type="+type+"dutyType="+dutyType);
        List<BasePersonInfo> basePersonInfos=guavacacheUtils.getPersons();
        List<ParamGroupDuty> paramGroupDuties=guavacacheUtils.getParamGroup();
        List<BaseGroupUserRelation> baseGroupUserRelations=guavacacheUtils.getUserGroupRelation();
        if (StringUtils.isEmpty(type)||StringUtils.isEmpty(dutyType)){
            return new ArrayList<>();
        }
        log.info("排班配置：customId="+customId+"type="+type+"dutyType="+dutyType);
        List<ParamGroupDuty> needed=paramGroupDuties.stream().filter(i->customId.equals(i.getCustomId())&&type.equals(i.getSeType())&&dutyType.equals(i.getSType()))
                .collect(Collectors.toList());
        log.info("排班配置获取到的群组信息："+ JSON.toJSONString(needed));
        if (CollectionUtils.isEmpty(needed)){
            return new ArrayList<>();
        }else {
            List<BaseGroupUserRelation> neededRelation=baseGroupUserRelations.stream().filter(i->needed.stream().map(ParamGroupDuty::getGroupId).collect(Collectors.toList())
            .contains(i.getGroupId())).collect(Collectors.toList());
            log.info("排班配置获取到的群组和人员关系："+ JSON.toJSONString(neededRelation));
            if (!CollectionUtils.isEmpty(neededRelation)){
                return basePersonInfos.stream().filter(i->neededRelation.stream().map(BaseGroupUserRelation::getUserId).collect(Collectors.toList()).contains(i.getUserno())).collect(Collectors.toList());
            }
        }
        return new ArrayList<>();
    }

    @Override
    public List<BasePersonInfo> getPersonInCustomGroupForNoG(String customId, String type, String dutyType) {
        return basePersonInfoMapper.getPersonInCustomGroupFotNoG(customId,type,dutyType);
    }
}
